<template>
	<view>
		<view class="container">
			<text class="text-lg">绑定手机号</text>
			<view class="row input-phone" >
				<zywork-icon type="icon-shouji" size="40" color="#919191"></zywork-icon>
				<text class="text-base" style="margin-left: 15rpx;">+86</text>
				<input style="margin-left: 15rpx;" placeholder="请输入手机号" type="number" v-model="phone"  pattern="[0-9]*"/>
			</view>
			
			<view  class="row input-phone" style="">
				<zywork-icon type="icon-yuechi" size="40" color="#919191"></zywork-icon>
				<input style="margin-left: 15rpx;width: 300rpx;" placeholder="请输入验证码" type="number" v-model="code"  pattern="[0-9]*"/>
				<text class="text-base " :class="getCodeDisabled?'font-color1':'font-color'" style="margin-left: auto;" @click="getCode">{{getCodeText}}</text>
			</view>
			<view class="login-button text-base" style="font-weight: 800;" @click="nextStep">
				绑定
			</view>	
		</view>
		<uni-popup ref="alertDialog" type="dialog">
			<uni-popup-dialog type="warning" title="客服电话" :buttonArr="['取消','拨打']" :content="phoneNumber" @confirm="dialogConfirm" @close="dialogClose"></uni-popup-dialog>
		</uni-popup>
	</view>
	
</template>

<script>
	export default {
		data() {
			return {
				phoneNumber:"400-668-6780",
				getCodeText:"获取验证码",
				getCodeDisabled:false,
				phone:"",
				code:"",
				userInfo:""
			};
		},
		onShow() {
			this.userInfo = uni.getStorageSync(this.$util.USER_INFO)
		},
		methods:{
			dialogConfirm() {
				console.log('拨打电话')
				uni.makePhoneCall({
				    phoneNumber: this.phoneNumber.replace(/\s+/g,"-")  //仅为示例
				});
			},
			dialogClose() {
				
			},
			nextStep(){
				let parameter = {phonenumber:this.phone,code:this.code}
				console.log(parameter)
				this.$util.doPostJson('/user/bindingPhone',parameter, {}, true).then(res => {
					console.log(res.data)
					if(res.data.code == this.$ResponseStatus.OK){
						this.$util.showInfoToast("绑定成功")
						this.timer = setTimeout(()=>{
							this.userInfo.phonenumber = parameter.phonenumber;
							uni.setStorageSync(this.$util.USER_INFO,this.userInfo)
							uni.navigateBack({})
						},1000)
					}
				}).catch(error => {
					console.log(error)
				})	
			},
			getCode() {
				if(this.getCodeDisabled){
					return
				}
				if (!this.$util.isPhone(this.phone)) {
					this.$util.showInfoToast('请输入正确的手机号')
					return
				}
				this.getCodeDisabled = true
				
				let self = this
				let time = 60
				this.$util.doPostForm('/loginApi/SendSMS', {phoneNumber: this.phone}, {}, false).then(res => {
					if(res.data.code == this.$ResponseStatus.ERROR){
						this.getCodeDisabled = false
					}else {
						let set = setInterval(()=> {
							self.getCodeText = '('+ --time +')秒后重新获取'
						}, 1000);
						this.time1 = setTimeout(()=> {
							self.getCodeDisabled = false;
							self.getCodeText = '获取验证码'
							clearInterval(set)
						}, time * 1000)
					}
					
				}).catch(error => {
					console.log(error)
				})
			}
		},
		onNavigationBarButtonTap(e) {
			 this.$refs.alertDialog.open()
		},
		beforeDestroy() {
			clearTimeout(this.timer)
		    this.timer = null;
			
			clearTimeout(this.time1)
			this.time1 = null;
		}
	}
</script>

<style lang="scss">
	.font-color{
		color: #6BA9D7;
	}
	
	.font-color1{
		color: #919191;
	}
	
	.container{
		padding: 30rpx 50rpx;
		margin-top: 80rpx;
		
		.input-phone{
			padding: 15rpx 0rpx;
			align-items: center;
			margin-top: 76rpx;
		}
		
		.login-button{
			flex: 1;
			background: $index-bg-bottom;
			color: #ffffff;
			height: 100rpx;
			text-align: center;
			line-height: 100rpx;
			border-radius: 50rpx;
			margin-top: 50rpx;
		}
		
	}
	
	.text-lg{
		font-size: 48rpx;
	}
	
	.text-base{
		font-size: 34rpx;
	}
</style>
